<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Home extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }

    /**
     * 要不要弹窗：TODO
     * type：1 关于我们  2 客服电话  3 隐私协议   4 派逊天地  5 平台换货地址  6 尺寸图  7 注册协议
     * @return string
     */
    public function tanYesNo()
    {
        extract(input());
        $tan = Db::table('up_u_user')->where('user_id',$user_id)->value('user_tan');
        if($tan == 1){
            Db::table('up_u_user')->where('user_id',$user_id)->setField('user_tan',2);
        }
        return json(['status'=>200,'msg'=>$tan,'dou'=>10]);
    }
    /**
     * 获取about表数据：
     * type：1 注册协议  2 隐私协议  3 关于我们   4 功能介绍  5 法律声明
     * @return string
     */
    public function getAbout()
    {
        extract(input());
        $res = Db::table('up_about')
            ->where('id',$type)
            ->find();
        return json(['status'=>200,'msg'=>$res]);
    }
    /**
     * location 自动定位返回id：location
     * $location  地区名称
     */
    public function locationGet (){
        extract(input());
        $res = array();
        if(isset($sheng)){
            $shengID = Db::table('up_area')
                ->where('district',$sheng)
                ->where('level',1)
                ->value('district_id');
            $res['sheng'] = $shengID;
        }
        if(isset($shi)){
            $shiID = Db::table('up_area')
                ->where('district',$shi)
                ->where('level',2)
                ->value('district_id');
            $res['shi'] = $shiID;
        }
        if(isset($qu)){
            $quID = Db::table('up_area')
                ->where('district',$qu)
                ->where('level',3)
                ->value('district_id');
            $res['qu'] = $quID;
        }
        return json(['status'=>200,'msg'=>$res]);
    }
    /**
     * 获取首页数据：getHomeData
     * 轮播图、四个商品
     */
    public function getHomeUp(){
        extract(input());
        $lunbo = Db::table('up_banner')
            ->where('banner_status',1)
            ->field(['banner_id','banner_title','banner_img','banner_link','banner_cate'])
            ->order('banner_sort ASC')
            ->page (1,5)
            ->select();
        //动态分类
        $types = Db::table('up_hot_type')
            ->where('type_status',1)
            ->field(['type_id','type_name','type_image'])
            ->order('type_sort ASC')
            ->select();
        Db::table('up_u_user')->where('user_id',$user_id)->setField('user_logintime',date('Y-m-d H:i:s'));
        return json(['status'=>200,'msg'=>$lunbo,'types'=>$types]);
    }
    /**
     * 获取首页数据：getHomeData
     * 轮播图、四个商品
     */
    public function getHomeData(){
        extract(input());
        $order = 'h_update_time desc';
        //动态
        $where = [
            'h_status' => 1 ,
        ];
        if(isset($type) && $type){
            $where['h_type'] = $type;
        }
        if(isset($keyword) && $keyword){
            $where['h_content'] = ['like',"%$keyword%"];
        }
        if(isset($hot) && $hot){ //type传空
            if($hot == 1){
                //热门，所有推荐的
                $order = 'h_top desc,h_message desc,h_update_time desc';
            }
            if($hot == 2){
                //只查看我关注的人发的
                $userids = Db::table('up_like')
                    ->where('l_user_id', $user_id)
                    ->column('l_other_user_id');
                if($userids){
                    $where['h_user_id'] = ['in',$userids];
                }
            }
        }
        $list = Db::table('up_hot h')
            ->join('up_u_user', 'h_user_id = user_id', 'left')
            ->field('h.*, user_username, user_image, user_time, user_id, user_level_name')
            ->where($where)
            ->page($page, 10)
            ->order($order)
            ->select();
        if($list){
            $list = $this->getHotsByData($list,$user_id);
        }
        return json(['status'=>200,'msg'=>$list]);
    }
    /**
     * 发布
     */
    public function userNewHot(){
        $h_content = trim(input('h_content'));
        $h_cate = input('h_cate');
        $h_image = trim(input('h_image'),',');
        $h_yinyan = trim(input('h_yinyan'));
        $h_address = trim(input('h_address'));
        $h_type = input('h_type');
        $user_id = input('user_id');

        $data = [
            'h_content' => $h_content,
            'h_cate' => $h_cate,
            'h_image' => $h_image,
            'h_yinyan' => $h_yinyan,
            'h_address' => $h_address,
            'h_user_id' => $user_id,
            'h_type' => $h_type,
            'h_time' => date('Y-m-d H:i:s'),
            'h_update_time' => date('Y-m-d H:i:s'),
            'h_date' => date('Y-m-d'),
        ];
        $res = Db::table('up_hot')->insertGetId($data);
        if($res){
            $have = Db::table('up_hot')
                ->where('h_user_id',$user_id)
                ->where('h_date',date('Y-m-d'))
                ->count();
            if($have <= 3){
                // 入记录
                allInRevenue(7,2,$user_id,$res);
            }
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    /**
     * 关注/取消关注
     * @return \think\response\Json
     * other_user_id    动态发布者ID
     */
    public function userLike(){
        $other_user_id = input('other_user_id');
        $user_id = input('user_id');
        if($other_user_id == $user_id){
            return json(['status' => 100, 'msg' => '不能关注自己']);
        }
        $have = Db::table('up_like')
            ->where('l_user_id', $user_id)
            ->where('l_other_user_id', $other_user_id)
            ->field('l_id')
            ->find();
        if($have){
            $res = Db::table('up_like')->where('l_id', $have['l_id'])->delete();
        }else{
            $insert = [
                'l_user_id' => $user_id,
                'l_other_user_id' => $other_user_id,
                'l_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_like')->insert($insert);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    /**
     * 收藏帖子
     * @return \think\response\Json
     */
    public function hotLike(){
        $h_id = input('h_id');
        $user_id = input('user_id');
        $have = Db::table('up_like_hot')
            ->where('lh_user_id', $user_id)
            ->where('lh_h_id', $h_id)
            ->find();
        if($have){
            $res = Db::table('up_like_hot')->where('lh_id', $have['lh_id'])->delete();
        }else{
            $insert = [
                'lh_user_id' => $user_id,
                'lh_h_id' => $h_id,
                'lh_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_like_hot')->insert($insert);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    /**
     * 点赞
     * 返参：
     * 1、$back_type：0都不亮 1赞亮 2踩亮
     * 2、$back_num：1赞加一，踩不动；2赞减一，踩不动；3踩加一，赞不动；4踩减一，赞不动；5赞加一 踩减一；6赞减一，踩加一
     */
    public function dianzan(){
        $today = date('Y-m-d H:i:s');
        $type = input('type'); //1是赞  2是踩
        $id = input('id'); //对应的id
        $from = input('from'); //1是一级评论  2是二级回复  3是动态（如果有3的话）
        $user_id = input('user_id');
        $have = Db::table('up_zan')
//            ->where('zan_type', $type)
            ->where('zan_com_type', $from)
            ->where('zan_user_id', $user_id)
            ->where('zan_com_id', $id)
            ->find();
        $update = [
            'zan_type' => $type,
            'zan_time' => $today
        ];
        if($have){
            //------------------加减数量  start  -------------------------------
            if($type == 1){ //如果是请求赞：原先也是赞和原先是踩
                if($have['zan_type'] == 1){
                    $res = Db::table('up_zan')->where('zan_id', $have['zan_id'])->delete();
                    $back_num = 2;
                    $back_type = 0;
                    if($from == 1){
                        Db::table('up_comment')->where('comment_id', $id)->setDec('comment_yes');
                    }elseif ($from == 2){
                        Db::table('up_comment_tt')->where('comtt_id', $id)->setDec('comtt_yes');
                    }elseif ($from == 3){
                        Db::table('up_hot')->where('h_id', $id)->setDec('h_yes');
                    }
                }else{
                    $res = Db::table('up_zan')->where('zan_id', $have['zan_id'])->update($update);
                    $back_num = 5;
                    $back_type = 1;
                    if($from == 1){
                        Db::table('up_comment')->where('comment_id', $id)->setDec('comment_no');
                        Db::table('up_comment')->where('comment_id', $id)->setInc('comment_yes');
                    }elseif ($from == 2){
                        Db::table('up_comment_tt')->where('comtt_id', $id)->setDec('comtt_no');
                        Db::table('up_comment_tt')->where('comtt_id', $id)->setInc('comtt_yes');
                    }elseif ($from == 3){
                        Db::table('up_hot')->where('h_id', $id)->setDec('h_no');
                        Db::table('up_hot')->where('h_id', $id)->setInc('h_yes');
                    }
                }

            }else{ //如果是请求踩：原先也是踩和原先是赞
                if($have['zan_type'] == 2){
                    $res = Db::table('up_zan')->where('zan_id', $have['zan_id'])->delete();
                    $back_num = 4;
                    $back_type = 0;
                    if($from == 1){
                        Db::table('up_comment')->where('comment_id', $id)->setDec('comment_no');
                    }elseif ($from == 2){
                        Db::table('up_comment_tt')->where('comtt_id', $id)->setDec('comtt_no');
                    }elseif ($from == 3){
                        Db::table('up_hot')->where('h_id', $id)->setDec('h_no');
                    }
                }else{
                    $res = Db::table('up_zan')->where('zan_id', $have['zan_id'])->update($update);
                    $back_num = 6;
                    $back_type = 2;
                    if($from == 1){
                        Db::table('up_comment')->where('comment_id', $id)->setDec('comment_yes');
                        Db::table('up_comment')->where('comment_id', $id)->setInc('comment_no');
                    }elseif ($from == 2){
                        Db::table('up_comment_tt')->where('comtt_id', $id)->setDec('comtt_yes');
                        Db::table('up_comment_tt')->where('comtt_id', $id)->setInc('comtt_no');
                    }elseif ($from == 3){
                        Db::table('up_hot')->where('h_id', $id)->setDec('h_yes');
                        Db::table('up_hot')->where('h_id', $id)->setInc('h_no');
                    }
                }
            }
            //------------------加减数量  end  -------------------------------
        }else{
            if($from == 1){
                $who = Db::table('up_comment')->where('comment_id', $id)->value('comment_reply_user_id');
            }elseif ($from == 2){
                $who = Db::table('up_comment_tt')->where('comtt_id', $id)->value('comtt_one_user_id');
            }elseif ($from == 3){
                $who = Db::table('up_hot')->where('h_id', $id)->value('h_user_id');
            }
            $insert = [
                'zan_type' => $type,
                'zan_com_type' => $from,
                'zan_user_id' => $user_id,
                'zan_com_id' => $id,
                'zan_who' => $who,
                'zan_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_zan')->insert($insert);
            //------------------加数量  start  -------------------------------
            if($type == 1){
                $back_num = 1;
                $back_type = 1;
                if($from == 1){
                    Db::table('up_comment')->where('comment_id', $id)->setInc('comment_yes');
                }elseif ($from == 2){
                    Db::table('up_comment_tt')->where('comtt_id', $id)->setInc('comtt_yes');
                }elseif ($from == 3){
                    Db::table('up_hot')->where('h_id', $id)->setInc('h_yes');
                }
            }else{
                $back_num = 3;
                $back_type = 2;
                if($from == 1){
                    Db::table('up_comment')->where('comment_id', $id)->setInc('comment_no');
                }elseif ($from == 2){
                    Db::table('up_comment_tt')->where('comtt_id', $id)->setInc('comtt_no');
                }elseif ($from == 3){
                    Db::table('up_hot')->where('h_id', $id)->setInc('h_no');
                }
            }
            //------------------加数量  end  -------------------------------
        }
        if($res){
            if($from == 1){
                $hid = Db::table('up_comment')->where('comment_id', $id)->value('comment_hot_id');
                $h_time = Db::table('up_hot')->where('h_id', $hid)->value('h_time');
                $this->setGod($hid,$h_time);
            }
            $this->sendDou($from,$id);
            return json(['status' => 200, 'msg' => '操作成功', 'back_num' => $back_num, 'back_type' => $back_type]);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    /**
     * 动态详情
     * @return \think\response\Json
     * h_id   动态ID
     * page   页码
     */
    public function HotDetail(){
        $h_id = input('h_id');
        $user_id = input('user_id');
        // 详情
        $detail = Db::table('up_hot h')
            ->join('up_u_user', 'h_user_id = user_id', 'left')
            ->field('user_username, user_image, user_id,user_time,h.*')
            ->where('h_id', $h_id)
            ->find();
        if($detail){
            $img = trim($detail['h_image'], ',');
            if($detail['h_cate'] == 1){
                if($img){
                    $detail['h_image'] = explode(',', $img);
                }else{
                    $detail['h_image'] = [];
                }
            }
            //有没有点赞，有没有收藏，有没有关注
            $detail['zan'] = 0;
            $detail['like_h'] = 0;
            $detail['like_u'] = 0;
            $zan = Db::table('up_zan')
                ->where('zan_com_type', 3)
                ->where('zan_user_id', $user_id)
                ->where('zan_com_id', $detail['h_id'])
                ->find();
            $like_h = Db::table('up_like_hot')
                ->where('lh_user_id', $user_id)
                ->where('lh_h_id', $detail['h_id'])
                ->find();
            $like_u = Db::table('up_like')
                ->where('l_user_id', $user_id)
                ->where('l_other_user_id', $detail['h_user_id'])
                ->find();
            if($zan){
                $detail['zan'] = $zan['zan_type'];
            }
            if($like_h){
                $detail['like_h'] = 1;
            }
            if($like_u){
                $detail['like_u'] = 1;
            }
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 100, 'msg' => '此动态已不存在']);
        }
    }

    /**
     * 动态里的评论列表
     * 非匿名下才展示昵称
     */
    public function getComments()
    {
        $h_id = input('h_id');
        $page = input('page');
        $user_id = input('user_id');
        $comment = Db::table('up_comment')
            ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
            ->field('user_id,user_username, user_image, comment_content, comment_num, comment_see,comment_yes,comment_no,comment_god,comment_id, comment_time')
            ->where('comment_hot_id', $h_id)
            ->order('comment_god desc,comment_num desc,comment_time desc')
            ->page($page, 10)
            ->select();
        if($comment){
            foreach ($comment as $k => $v){
                $comment[$k]['zan'] = 0;
                $zan = Db::table('up_zan')
                    ->where('zan_com_type', 1)
                    ->where('zan_user_id', $user_id)
                    ->where('zan_com_id', $v['comment_id'])
                    ->find();
                if($zan){
                    $comment[$k]['zan'] = $zan['zan_type'];
                }
            }
        }
        return json(['status' => 200, 'msg' => $comment]);
    }
    /**
     * 评论详情
     * @return \think\response\Json
     * comment_id    评论ID
     * comtt_see为1时显示头像，2时匿名
     */
    public function commentDetail(){
        $comment_id = input('comment_id');
        $user_id = input('user_id');
        $detail = Db::table('up_comment c')
            ->join('up_u_user u', 'u.user_id = c.comment_reply_user_id', 'left')
            ->field('user_username, user_image,user_id, c.*')
            ->where('comment_id', $comment_id)
            ->find();
        if($detail){
            $detail['zan'] = 0;
            $zan = Db::table('up_zan')
                ->where('zan_com_type', 1)
                ->where('zan_user_id', $user_id)
                ->where('zan_com_id', $detail['comment_id'])
                ->find();
            if($zan){
                $detail['zan'] = $zan['zan_type'];
            }
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 100, 'msg' => '此动态已不存在']);
        }
    }
    /**
     * 评论里的回复
     * @return \think\response\Json
     * comment_id    评论ID
     * page    页码
     * 如果comtt_reply_user_id不为0说明是回复的回复，否则代表回复的是评论
     * 如果comtt_see为1时显示回复者的头像
     * 被回复者：comtt_reply_see为1时且comtt_reply_user_id不为0正常显示，comtt_reply_see为2时显示匿名，其他代表没有被回复者
     */
    public function commentReplays(){
        $comment_id = input('comment_id');
        $page = input('page');
        $user_id = input('user_id');

        $field = 'u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name';
        $field .= ',comtt_id,comtt_content,comtt_time,u.user_image one_image,u2.user_image reply_image';
        $field .= ',comtt_see,comtt_reply_see,comtt_yes,comtt_no';
        $data = Db::table('up_comment_tt')->alias('m')
            ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
            ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
            ->where('comtt_comment_id',$comment_id)
            ->field($field)
            ->order('comtt_time DESC')
            ->page($page,10)
            ->select();
        if($data){
            foreach ($data as $k => $v){
                $data[$k]['zan'] = 0;
                $zan = Db::table('up_zan')
                    ->where('zan_com_type', 2)
                    ->where('zan_user_id', $user_id)
                    ->where('zan_com_id', $v['comtt_id'])
                    ->find();
                if($zan){
                    $data[$k]['zan'] = $zan['zan_type'];
                }
            }
        }
        return json(['status' => 200, 'msg' => $data]);
    }
    /**
     * 转发成功加 1
     * @return string
     */
    public function hotZhuan()
    {
        $h_id = input('h_id');
        $res = Db::table('up_hot')->where('h_id', $h_id)->setInc('h_zhuan');
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    /**
     * 生成省市区json数据：makeJson
     * @return string
     */
    public function makeJson()
    {
//        set_time_limit(0);
//        $area = file_get_contents('static/area.json');
//        $area2 = json_decode($area,true);
//        return json($area2);
        set_time_limit(0);
        $pro = Db::table('up_area')
            ->where('pid',0)
//            ->where('district_id',1001)
            ->field('district_id id,district name')
            ->order('sort')
            ->select();
        foreach($pro as $k=>$v){
            $pro[$k]['parentId'] = 0;
            $city = Db::table('up_area')
                ->where('pid',$v['id'])
                ->where('level',2)
                ->field('district_id id,district name,pid parentId')
                ->select();
            foreach ($city as $kk=>$vv){
                $city[$kk]['sub'] = Db::table('up_area')
                    ->where('pid',$vv['id'])
                    ->where('level',3)
                    ->field('district_id id,district name,pid parentId')
                    ->select();
            }
            $pro[$k]['sub'] = $city;
        }
        return json($pro);
    }

}